package at.fhj.itm10.ticketmaster.services.impl;

import java.util.List;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.dao.AddressDAO;
import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.entities.Address;
import at.fhj.itm10.ticketmaster.helper.Validator;
import at.fhj.itm10.ticketmaster.services.AddressService;
import at.fhj.itm10.ticketmaster.services.ServiceException;

/**
 * {@inheritDoc}
 * 
 * @author Sabrina Gruber
 * @author Tammo Böhme
 */

public class AddressServiceImpl extends AbstractService implements AddressService {
	
	private Logger logger = Logger.getLogger(AddressServiceImpl.class);
		
	private AddressDAO addressDao;

	public void setAddressDao(final AddressDAO theDao) {
		this.addressDao = theDao;
	}

	public AddressDAO getAddressDao() {
		return addressDao;
	}

	public void insertAddress(Address address) {
		logger.debug("insertAddress()");

		Validator.notNullOrThrow(address, "address is null");		
		Validator.zeroOrThrow(address.getId(),
				"id for new customer must be zero - will be autofilled");

		try {
			begin();

			getAddressDao().insert(address);
			
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on inserting data", e);
		} finally {
			closeConnection();
		}
	}

	public void updateAddress(Address address) {
		logger.debug("updateAddress()");
		
		Validator.notNullOrThrow(address, "address is null");
		Validator.notZeroOrThrow(address.getId(), "id must be given for update");
		
		try {
			begin();
			getAddressDao().update(address);
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on updating data", e);
		} finally {
			closeConnection();
		}
	}

	public void deleteAddress(Address address) {
		logger.debug("deleteAddress()");
		
		Validator.notNullOrThrow(address, "address is null");
		Validator.notZeroOrThrow(address.getId(), "id must be given for delete");

		try {
			begin();
			getAddressDao().delete(address);
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on deleting data", e);
		} finally {
			closeConnection();
		}
	}

	public Address findById(long id) {
		logger.debug("findById()");

		Address address = null;
		try {
			begin();
			address = getAddressDao().findById(id);
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on retrieving data", e);
		} finally {
			closeConnection();
		}
		
		return address;
	}
	
	public List<Address> findAll() {
		logger.debug("findAll()");

		List<Address> address = null;
		try {
			begin();
			address = getAddressDao().findAll();
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on retrieving data", e);
		} finally {
			closeConnection();
		}
		
		return address;
	}
}
